<?php
namespace Admin\Controller;
use Think\ControllerEx;
class UserController extends CommonController {
    public function _initialize(){
        $this->base_init('User');
     }

    //查看各支行情况
    public function index(){
    	$userTable = M('User');
      $where['role']  = '0';
    	$userRes   = $userTable->where($where)
                               ->field('uid,username,branchname,email,tel,is_report,is_receive,status,datetime')
                               ->select();
    	//dump($userRes);die;
    	$this->assign('userList',$userRes);
		$this->display('user-list');
    }

    /**
     * 获取各支行信息 除admin
     * @return [type] [description]
     */
    public function getUser(){
    	$userTable = M('User');
    	$where['role']  = '0';
    	$where['status']  = '1';

    	$userRes = $userTable->where($where)
    						->field('uid,branchname')	
    						->select();  						
    	return $userRes;
    }

    /**
     * 密码重置方法
     * @return 成功 返回1 失败返回0 
     */
    public function psReset($uid='0'){
        $uid = I('uid');
        $userTable = M('User');

        $where['uid'] = I('uid');
        $date['password'] = '111111';

        $res = $userTable->where($where)->setField($date); 
        //echo $userTable->getLastsql();
        if ($res !== false ) {
            $this->ajaxReturn('1');
        }else{
            $this->ajaxReturn('0');
        }
    }

    /**
     * 修改用户状态 为停用
     * @return 成功 返回1 失败返回0 
     */
    public function staStop($uid='0'){
        $uid = I('uid');
        $userTable = M('User');

        $where['uid'] = I('uid');
        $date['status'] = '0';

        $res = $userTable->where($where)->setField($date); 
        //echo $userTable->getLastsql();
        if ($res !== false ) {
            $this->ajaxReturn('1');
        }else{
            $this->ajaxReturn('0');
        }
    }

    /**
     * 修改用户状态 为正常
     * @return 成功 返回1 失败返回0 
     */
    public function staStart(){
        $uid = I('uid');
        $userTable = M('User');

        $where['uid'] = I('uid');
        $date['status'] = '1';

        $res = $userTable->where($where)->setField($date); 
        //echo $userTable->getLastsql();
        if ($res !== false ) {
            $this->ajaxReturn('1');
        }else{
            $this->ajaxReturn('0');
        }
    }

    /**
     * 执行添加用户方法
     */
    public function doUserAdd(){
        //$this->ajaxReturn('1');die();
        $userTab = M('user');
        $date    =  I();
       /* echo $userTab->_sql();
        dump($date);*/
        $userRes = $userTab->data()->add($date);
        if ($userRes) {
            $this->ajaxReturn('1');
        }else{
            $this->ajaxReturn('0');
        }
    }

     /**
       * 查看 上报状态
       * return $arr 没有上报的uid name
       */
      public function getNoreport()
      { 
        $where['status'] = 1;
        $where['role']   = 0;
        $where['is_report'] = array('NEQ',date('Ym'));
        
        $userRes = $this->dbTable ->where($where)->field('uid,branchname')->select();
        if ($userRes) {
          return $userRes;
        }else{
          return false;
        }
      }

    //修改密码
    public function changePs(){
        $this->display('change-password');
    }

    //
    public function uppass(){
        $date['password'] = I('newpassword');
        $pass2            = I('newpassword2');

        $where['uid'] = session('admin')['uid'];
        //echo($where['uid']);die;
        if ($date['password'] == $pass2) {

            $userRes = $this->dbTable->where($where)->setField($date);
            if ($userRes !== false) {
                $this->success('密码修改成功！');
            }else{
                //echo $userTab->getLastsql();die();
                $this->error('修改失败，请重试！');
            }
        }
    }


}